package org.filehanding.validator;

import org.apache.commons.lang3.StringUtils;
import org.filehanding.entity.User;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * Created by liguo on 2016/5/14.
 */
public class UserValidator implements Validator {
    public boolean supports(Class<?> aClass) {
        return User.class.equals(aClass);
    }

    public void validate(Object o, Errors errors) {
        User user = (User) o;


        if (StringUtils.isEmpty(user.getName())) {
            errors.reject("姓名不能为空");
        } else if (user.getName().length() < 2) {
            errors.reject("姓名长度不能小于2");
        } else if (user.getName().length() > 10) {
            errors.reject("姓名长度不能大于10");
        }

        if (StringUtils.isEmpty(user.getPassword())) {
            errors.reject("密码不能为空");
        } else if (user.getPassword().length() < 6) {
            errors.reject("密码长度不能小于6");
        } else if (user.getPassword().length() > 16) {
            errors.reject("密码长度不能大于16");
        }

        if (StringUtils.isEmpty(user.getRePassword())) {
            errors.reject("密码重复不能为空");
        } else if (!user.getPassword().equals(user.getRePassword())) {
            errors.reject("两次输入密码不一致");
        }




    }
}
